package cc.admin.modules.sys.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.aspect.annotation.AutoLog;
import cc.admin.common.sys.base.controller.BaseController;
import cc.admin.common.sys.query.QueryGenerator;
import cc.admin.modules.sys.entity.SysShip;
import cc.admin.modules.sys.service.ISysShipService;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 船舶表
 * @Author: cc-admin
 * @Date: 2023-09-14
 * @Version: V1.0.0
 */
@Slf4j
@Api(tags = "船舶表")
@RestController
@RequestMapping("/sys/ship")
public class SysShipController extends BaseController<SysShip, ISysShipService> {
	@Autowired
	private ISysShipService sysShipService;

	@AutoLog(value = "船舶表-分页列表查询")
	@ApiOperation(value = "船舶表-分页列表查询", notes = "船舶表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(
			SysShip sysShip,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
			HttpServletRequest req) {
		String shipName = sysShip.getShipName();
		if (StrUtil.isNotEmpty(shipName)) {
			sysShip.setShipName("*" + shipName + "*");
		}
		QueryWrapper<SysShip> queryWrapper = QueryGenerator.initQueryWrapper(sysShip, req.getParameterMap());
		Page<SysShip> page = new Page<>(pageNo, pageSize);
		IPage<SysShip> pageList = sysShipService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	@AutoLog(value = "船舶表-分页列表查询")
	@ApiOperation(value = "船舶表-分页列表查询", notes = "船舶表-分页列表查询")
	@GetMapping(value = "/queryShipList")
	public Result<?> queryShipList(
			@RequestParam(name = "isSys", required = false) String isSys,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
			HttpServletRequest req) {
		QueryWrapper<SysShip> queryWrapper = new QueryWrapper<>();
		if (StrUtil.isNotEmpty(isSys)) {
			queryWrapper.eq("is_sys", isSys);
		}
		queryWrapper.orderByAsc("create_time");
		List<SysShip> sysShipList = sysShipService.list(queryWrapper);
		return Result.ok(sysShipList);
	}

	/**
	 * 添加
	 *
	 * @param sysShip
	 * @return
	 */
	@AutoLog(value = "船舶表-添加")
	@ApiOperation(value = "船舶表-添加", notes = "船舶表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody SysShip sysShip) {
		sysShip.setStatus("未处理");
		sysShipService.save(sysShip);
		return Result.ok("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param sysShip
	 * @return
	 */
	@AutoLog(value = "船舶表-编辑")
	@ApiOperation(value = "船舶表-编辑", notes = "船舶表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody SysShip sysShip) {
		sysShipService.updateById(sysShip);
		return Result.ok("编辑成功!");
	}

	/**
	 * 编辑是否体系船舶
	 *
	 * @param sysShip
	 * @return
	 */
	@AutoLog(value = "船舶表-编辑")
	@ApiOperation(value = "船舶表-编辑", notes = "船舶表-编辑")
	@PutMapping(value = "/changeIsSys")
	public Result<?> changeIsSys(@RequestBody SysShip sysShip) {
		UpdateWrapper<SysShip> updateWrapper = new UpdateWrapper<>();
		String isSys = "0".equals(sysShip.getIsSys()) ? "1" : "0";
		String opt = "0".equals(isSys) ? "禁用" : "启用";
		updateWrapper.set("is_sys", isSys);
		updateWrapper.eq("id", sysShip.getId());
		sysShipService.update(updateWrapper);
		return Result.ok(String.format("%s成功!", opt));
	}

	/**
	 * 复制
	 *
	 * @param id
	 * @param name
	 * @return
	 */
	@AutoLog(value = "船舶表-复制")
	@ApiOperation(value = "船舶表-复制", notes = "船舶表-复制")
	@PostMapping(value = "/copy")
	public Result<?> copy(@RequestParam(name = "id", required = true) String id, @RequestParam(name = "name", required = true) String name) {
		SysShip sysShip = sysShipService.getById(id);
		sysShip.setId(IdUtil.fastUUID());
		sysShip.setCreateTime(new Date());
		sysShipService.save(sysShip);
		return Result.ok("复制成功！");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "船舶表-通过id删除")
	@ApiOperation(value = "船舶表-通过id删除", notes = "船舶表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		sysShipService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "船舶表-批量删除")
	@ApiOperation(value = "船舶表-批量删除", notes = "船舶表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.sysShipService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "船舶表-通过id查询")
	@ApiOperation(value = "船舶表-通过id查询", notes = "船舶表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		SysShip sysShip = sysShipService.getById(id);
		return Result.ok(sysShip);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param sysShip
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, SysShip sysShip) {
		return super.exportXls(request, sysShip, SysShip.class, "船舶表");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, SysShip.class);
	}

}
